package com.gusha.service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.TrainCommDao;

import com.gusha.model.SysTraining;
import com.gusha.model.TrainComm;
import com.gusha.model.User;

@Service
@Transactional
public class TrainCommService {
	@Autowired
	private TrainCommDao trainCommDao;
	
	public Page<TrainComm> findAll(Pageable pageable, TrainComm trainComm) {
        @SuppressWarnings("serial")
        Page<TrainComm> result = trainCommDao.findAll(new Specification<TrainComm>() {
            @Override
            public Predicate toPredicate(Root<TrainComm> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (trainComm.getId() != null) {
                    list.add(cb.equal(root.get("id").as(BigInteger.class),trainComm.getId()));
                }
                if (trainComm.getUser() != null) {
                    list.add(cb.equal(root.get("user").as(User.class),trainComm.getUser()));
                }
                if (trainComm.getSysTraining() != null) {
                    list.add(cb.equal(root.get("sysTraining").as(SysTraining.class),trainComm.getSysTraining()));
                }
                if (trainComm.getCommContent() != null) {
                    list.add(cb.equal(root.get("commContent").as(String.class),trainComm.getCommContent()));
                }
                if (trainComm.getTrainTime() != null) {
                    list.add(cb.equal(root.get("trainTime").as(Date.class),trainComm.getTrainTime()));
                }
                if (trainComm.getIsDelete() != null) {
                    list.add(cb.equal(root.get("isDelete").as(BigInteger.class),trainComm.getIsDelete()));
                }
                if (trainComm.getIsCheck() != null) {
                    list.add(cb.equal(root.get("isCheck").as(BigInteger.class),trainComm.getIsCheck()));
                }
                if (trainComm.getState() != null) {
                    list.add(cb.equal(root.get("state").as(String.class),trainComm.getState()));
                }
                if (trainComm.getParentsId() != null) {
                    list.add(cb.equal(root.get("parentsId").as(TrainComm.class),trainComm.getParentsId()));
                }
                
                
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
         return result;
    }


}
